package com.hulk.Idempotent;



import com.hulk.Idempotent.annotation.EnableLimitbConfig;

import com.hulk.Idempotent.util.LimitbKeyParser;
import com.hulk.Idempotent.util.SpringContextHolder;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 *  configuration
 *
 * @author hulk
 */
@Configuration
@ConditionalOnClass({RedisConnectionFactory.class})
@ConditionalOnBean(annotation = EnableLimitbConfig.class)
@AllArgsConstructor
public class LimitbAutoConfiguration {


   private  RedisConnectionFactory redisConnectionFactory;

   @Bean(value = "limitbRedisTemplate")
   public RedisTemplate<String, Object> limitbRedisTemplate() {
      RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
      redisTemplate.setKeySerializer(new StringRedisSerializer());
      redisTemplate.setHashKeySerializer(new StringRedisSerializer());
      redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
      redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
      redisTemplate.setConnectionFactory(redisConnectionFactory);
      return redisTemplate;
   }

   @Bean
   public LimitbKeyParser limitbKeyUtil (){
      LimitbKeyParser provider = new LimitbKeyParser();
      return  provider;
   }


   @Bean
   public LimitbInterceptor limitbInterceptor ( LimitbKeyParser limitbKeyParser){
      LimitbInterceptor interceptor = new LimitbInterceptor(limitbKeyParser);
      return  interceptor;
   }
   @Bean
   public SpringContextHolder springContextHolder (){
      SpringContextHolder springContextHolder = new SpringContextHolder();
      return  springContextHolder;
   }


}
